from pages.basePage1 import BasePage
import time
import allure
import os
from config.setting import host_port

# 定位层
class UserPage(BasePage):
    def __init__(self):
        BasePage.__init__(self)
        self.driver.get(f'http://{host_port}/SharedParkingPlace/tenant/index.html')
        time.sleep(1)
    # 定位快捷栏 [我的]
    def userInfo(self):
        return self.driver.find_element_by_link_text('我的')
    # 定位快捷栏 [抢租客0]
    def qzk0(self):
        return self.driver.find_element_by_link_text('抢租客0')
    # 定位快捷栏 [登出]
    def logout(self):
        return self.driver.find_element_by_xpath('//button[@onclick="logout()"]')
    # 定位按钮 [更改信息]
    def changeInfo(self):
        return self.driver.find_element_by_xpath('//div[@class="panel panel-default"]/div[2]/div/h3/button[1]')
    # 定位显示框 [用户姓名]
    def displayName(self):
        return self.driver.find_element_by_xpath('//div[@id="main"]//div[@class="panel-body"]//h3/p[1]')
    # 定位显示框[用户电话]
    def displayPhone(self):
        return self.driver.find_element_by_xpath('//div[@id="main"]//div[@class="panel-body"]//h3/p[2]')
    # 定位显示框[用户昵称]
    def displayNickName(self):
        return self.driver.find_element_by_xpath('//div[@id="main"]//div[@class="panel-body"]//h3/p[3]')
    # 定位显示框[用户性别]
    def displaySex(self):
        return self.driver.find_element_by_xpath('//div[@id="main"]//div[@class="panel-body"]//h3/p[4]')
    # 定位信息框 [更改信息]-[用户姓名]
    def userName(self):
        return self.driver.find_element_by_xpath('//input[@id="uname"]')
    # 定位信息框 [更改信息]-[用户电话]
    def userPhone(self):
        return self.driver.find_element_by_xpath('//input[@id="phone"]')
    # 定位信息框 [更改信息]-[用户昵称]
    def userNickName(self):
        return self.driver.find_element_by_xpath('//input[@id="phone"]')
    # 定位信息框 [更改信息]-[用户性别]
    def userSex(self):
        return self.driver.find_element_by_xpath('//input[@id="sex"]')
    # 定位按钮 [提交更改]
    def submitChangeUser(self):
        return self.driver.find_element_by_xpath('//div[@id="userinfo"]//div[@class="modal-footer "]/button[2]')
    # 定位按钮 [实名认证]
    def nameCertification(self):
        return self.driver.find_element_by_xpath('//div[@class="panel panel-default"]/div[2]/div/h3/button[2]')
    # 定位按钮 [更改头像]
    def changeHead(self):
        return self.driver.find_element_by_xpath('//div[@class="panel panel-default"]/div[2]/div/h3/button[3]')
    # 定位按钮 [选择文件]
    def upFile(self):
        return self.driver.find_element_by_xpath('//input[@id="cert"]')
    # 定位按钮 [上传]
    def btnUpload(self):
        return self.driver.find_element_by_xpath('//button[@onclick="submit2()"]')
    # 定位按钮 [变更绑定]
    def changeBind(self):
        return self.driver.find_element_by_xpath('//h3[@class="panel-title"]/div/div/button[1]')
    # 定位输入框 [更改绑定]-[绑定阿里账户]
    def aliInput(self):
        return self.driver.find_element_by_xpath('//input[@id="bindalipay"]')
    # 定位按钮 [更改绑定]-[提交更改]
    def submitChangeAli(self):
        return self.driver.find_element_by_xpath('//div[@id="account"]//div[@class="modal-footer "]/button[2]')
    # 定位按钮 [历史操作]
    def historyOpt(self):
        return self.driver.find_element_by_xpath('//h3[@class="panel-title"]/div/div/button[2]')
    # 定位按钮 [账户充值]
    def acCharge(self):
        return self.driver.find_element_by_xpath('//h3[@class="panel-title"]/div/div/button[3]')
    # 定位输入框 [账户充值]-[付款金额]
    def payAmount(self):
        return self.driver.find_element_by_xpath('//input[@id="WIDtotal_amount"]')
    # 定位按钮 [付款]
    def payment(self):
        return self.driver.find_element_by_xpath('//button[@type="submit"]')
    # 定位显示框 [账户余额]
    def acBalance(self):
        return self.driver.find_element_by_xpath('//div[@class="panel panel-default"][2]//div[2]//h3//p[2]')
    # 定位显示框 [阿里账户]
    def acAli(self):
        return self.driver.find_element_by_xpath('//div[@class="panel panel-default"][2]//div[2]//h3//p[3]')
    # 定位按钮 [生成分享码]
    def sharedCode(self):
        return self.driver.find_element_by_xpath('//button[@align="center"]')

# 操作层
class UserHandle(UserPage):
    def __init__(self):
        UserPage.__init__(self)

    # 切换表单
    @allure.step('切换表单')
    def switchFrame(self):
        self.driver.switch_to.frame('testIframe')

    # 点击快捷栏 [我的]
    @allure.step('点击快捷栏[我的]')
    def clickUserInfo(self):
        element = self.userInfo()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击 [抢租客0]
    @allure.step('点击 [抢租客0]')
    def clickQzk0(self):
        element = self.qzk0()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击 [登出]
    @allure.step('点击 [登出]')
    def clickLogout(self):
        element = self.logout()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击 [更改信息]
    @allure.step('点击 [更改信息]')
    def clickChangeInfo(self):
        element = self.changeInfo()
        self.driver.execute_script('arguments[0].click();', element)

    # 清空 输入框[用户姓名]
    @allure.step('输入框 [用户姓名]')
    def clearUserName(self):
        self.userName().clear()

    # 清空 输入框[用户电话]
    @allure.step('输入框 [用户电话]')
    def clearUserPhone(self):
        self.userPhone().clear()

    # 清空 输入框[用户昵称]
    @allure.step('输入框 [用户昵称]')
    def clearNickName(self):
        self.userNickName().clear()

    # 清空 输入框[用户性别]
    @allure.step('输入框 [用户性别]')
    def clearUserSex(self):
        self.userSex().clear()

    # 输入 [用户姓名]
    @allure.step('输入 [用户姓名]')
    def inputUserName(self, name):
        self.userName().send_keys(name)

    # 输入 [用户电话]
    @allure.step('输入 [用户电话]')
    def inputUserPhone(self, phone):
        self.userPhone().send_keys(phone)

    # 输入 [用户昵称]
    @allure.step('输入框 [用户昵称]')
    def inputNickName(self, nickName):
        self.userNickName().send_keys(nickName)

    # 输入 [用户性别]
    @allure.step('输入框 [用户性别]')
    def inputUserSex(self, sex):
        self.userSex().send_keys(sex)

    # 获取 [用户姓名]
    @allure.step('获取[用户姓名]')
    def getName(self):
        return self.displayName().text

    # 获取 [用户电话]
    @allure.step('获取[用户电话]')
    def getPhone(self):
        return self.displayPhone().text

    # 获取 [用户昵称]
    @allure.step('获取[用户昵称]')
    def getNickName(self):
        return self.displayNickName().text

    # 获取 [用户性别]
    @allure.step('获取[用户性别]')
    def getSex(self):
        return self.displaySex().text

    # 点击 [提交更改]
    @allure.step('点击[提交更改]')
    def clickSubmitChange(self):
        element = self.submitChangeUser()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击 [实名认证]
    @allure.step('点击[实名认证]')
    def clickNameCertification(self):
        element = self.nameCertification()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击 [更改头像]
    @allure.step('点击[更改头像]')
    def clickChangeHand(self):
        element = self.changeHead()
        self.driver.execute_script('arguments[0].click();', element)

    # 上传文件
    @allure.step('上传文件')
    def clickUpFile(self, pngPath):
        basePath = os.path.abspath(pngPath)
        self.upFile().send_keys(basePath)

    # 点击 [上传]
    @allure.step('点击[上传]')
    def clickBtnUpLoad(self):
        element = self.btnUpload()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击 [变更绑定]
    @allure.step('点击[变更绑定]')
    def clickChangeBind(self):
        element = self.changeBind()
        self.driver.execute_script('arguments[0].click();', element)

    # 在输入框 [绑定阿里账户] 中输入阿里账户
    @allure.step('在输入框[绑定阿里账户]中输入阿里账户')
    def inputAli(self, aliAccount):
        self.aliInput().send_keys(aliAccount)

    # 点击 [提交更改]
    @allure.step('点击[提交更改]')
    def clicksubmitAli(self):
        element = self.submitChangeAli()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击 [历史操作]
    @allure.step('点击[历史操作]')
    def clickHistoryOpt(self):
        element = self.historyOpt()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击 [账户充值]
    @allure.step('点击[账户充值]')
    def clickAcCharge(self):
        element = self.acCharge()
        self.driver.execute_script('arguments[0].click();', element)

    # 输入 [付款金额]
    @allure.step('输入[付款金额]')
    def inputPayAmount(self, money):
        self.payAmount().send_keys(money)

    # 点击 [付款]
    @allure.step('点击[付款]')
    def clickPayment(self):
        element = self.payment()
        self.driver.execute_script('arguments[0].click();', element)

    # 提取 [账户余额]
    @allure.step('提取[账户余额]')
    def getAcBalance(self):
        result = self.acBalance().text
        return result

    # 提取 [阿里账户] 内容
    @allure.step('提取[阿里账户]内容')
    def getAcAli(self):
        result = self.acAli().text
        return result

    # 点击 [生成分享码]
    @allure.step('点击[生成分享码]')
    def clickSharedCode(self):
        element = self.sharedCode()
        self.driver.execute_script('arguments[0]/click();', element)

    # 切换到警告框，获取alert警告框文本内容,并点击警告框内的确定按钮
    @allure.step('切换到警告框')
    def getAlertText(self):
        alertObj = self.driver.switch_to.alert
        alertText = alertObj.text

        return alertText, alertObj

# 业务层
class UserBusiness(UserHandle):
    def __init__(self):
        UserHandle.__init__(self)

    # 抢租客-我的-更改信息-输入更改信息-提交更改-断言
    def optChangeInfo(self, name, phone, nickName, sex):
        time.sleep(1)
        # 点击我的
        self.clickUserInfo()
        # 切换表单
        self.switchFrame()
        # 点击[更改信息]
        self.clickChangeInfo()
        # 清空信息
        self.clearUserName()
        self.clearUserPhone()
        self.clearNickName()
        self.clearUserSex()
        # 输入更改信息
        self.inputUserName(name)
        self.inputUserPhone(phone)
        self.inputNickName(nickName)
        self.inputUserSex(sex)
        time.sleep(2)
        # 点击[提交更改]
        self.clickSubmitChange()
        # 返回断言信息
        name = self.getName()
        phone = self.getPhone()
        nickName = self.getNickName()
        sex = self.getSex()

        # print(name, phone, nickName, sex)

        # self.driver.close()
        return name, phone, nickName, sex

    # 抢租客-我的-更改头像-上传文件-上传-断言
    def optChangeHead(self, pngPath):
        time.sleep(1)
        # 进入[我的]模块
        self.clickUserInfo()
        # 切换表单
        self.switchFrame()
        # 点击[更换头像]
        self.clickChangeHand()
        # 选择文件
        self.clickUpFile(pngPath)
        time.sleep(1)
        # 点击上传
        self.clickBtnUpLoad()
        self.driver.find_element_by_xpath('//button[@onclick="submit2()"]').click()
        time.sleep(1)
        # 利用警告框断言，切换到警告框，获取警告框文本
        # alertObject = self.driver.switch_to.alert
        # alertText = alertObject.text
        # time.sleep(1)
        # alertObject.accept()
        # self.driver.close()
        alertText, alertObj = self.getAlertText()
        time.sleep(2)
        alertObj.accept()
        time.sleep(1)
        # self.driver.quit()

        return alertText

    # 抢租客-我的-变更绑定-输入绑定账号-提交更改-断言
    def optChangeBind(self, aliAccount):
        time.sleep(1)
        # 进入[我的]模块
        self.clickUserInfo()
        # 切换表单
        self.switchFrame()
        # 点击[变更绑定]
        self.clickChangeBind()
        time.sleep(1)
        # 清空输入框
        self.aliInput().clear()
        time.sleep(1)
        # 输入阿里账号
        self.inputAli(aliAccount)
        # 点击[提交更改]
        self.submitChangeAli()
        time.sleep(2)
        # 获取断言
        asserts = self.acAli().text
        # self.driver.close()

        return asserts, aliAccount


    # 抢租客-我的-账户充值-充值金额-充值-断言
    def optRecharge(self, money):
        time.sleep(1)
        # 进入[我的]模块
        self.clickUserInfo()
        # 切换表单
        self.switchFrame()
        # 点击 [账户充值]
        self.clickAcCharge()
        # 清除付款金额
        self.payAmount().clear()
        # 输入付款金额
        self.inputPayAmount(money)
        # 点击[付款]
        self.clickPayment()

        # self.driver.close()

        return '断言失败'

if __name__ == '__main__':
    obj = UserBusiness()
    # obj.optChangeInfo('王路飞', '18291894583', '大西瓜', '01')
    # obj.optChangeHead('../source/zelda2.png')
    # obj.optChangeBind('3333333')
    obj.optRecharge('100')
